package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.ProductSpecService;
import com.atguigu.spzx.model.entity.product.ProductSpec;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author syh
 * @create 2023-08-22 21:19
 */
@RestController
@Tag(name = "商品规格管理")
@RequestMapping(value="/admin/product/spec")
public class ProductSpecController {

    @Autowired
    private ProductSpecService productSpecService ;

    @GetMapping("findAll")
    @Operation(summary = "加载商品规格")
    public Result findAll() {
        List<ProductSpec> list = productSpecService.findAll();
        return Result.build(list , ResultCodeEnum.SUCCESS) ;
    }

    @Operation(summary = "更新商品规格")
    @PutMapping()
    public Result Update(@RequestBody ProductSpec productSpec) {
        productSpecService.updateById(productSpec);
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    @Operation(summary = "新增商品规格")
    @PostMapping()
    public Result Save(@RequestBody ProductSpec productSpec) {
        productSpecService.Save(productSpec);
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    @Operation(summary = "删除商品规格")
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable("id")Long id) {
      productSpecService.deleteById(id);
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    @Operation(summary = "分页查询商品规格")
    @GetMapping("page/{pageNum}/{pageSize}")
    public Result<PageInfo<ProductSpec>> findByPage( @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        PageInfo<ProductSpec> pageInfo = productSpecService.findByPage(pageNum, pageSize);
        return Result.build(pageInfo , ResultCodeEnum.SUCCESS) ;
    }


}
